/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：ScrollableMenuScreen.java <br>
 * 创建时间：2013-12-22 下午5:53:02 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.core;

import lostland.gmud.exv2.ui.AutoButton;

/**
 * 类名：ScrollableMenuScreen <br>
 * 说明：
 *
 * @author 12548
 */
public abstract class ScrollableMenuScreen extends ButtonControlledScreen {


	protected int max;
	protected int x, y;

	protected SMBorder nborder;
	public AutoButton buttons[];

	boolean showmax;

	public int cursor = 0;

	protected int hcursor = 0;

	public abstract void drawbg();

	@Override
	public abstract void onCancel();

	public abstract void onClick(int i);


	public ScrollableMenuScreen(String[] s, int x, int y, int width, int max, boolean showmax) {
		super();
		this.x = x;
		this.y = y;
		this.max = showmax ? max : Math.min(max, s.length);
		nborder = new SMBorder(x, y, width, this.max * 12 + 2);
		buttons = new AutoButton[s.length];
		for (int i = 0; i < buttons.length; i++) {
			buttons[i] = new AutoButton(x + 1, y + 1 + 12 * i, s[i]);
		}

		this.showmax = showmax;

	}

	protected void re(String[] s, int x, int y, int width, int max) {
		this.x = x;
		this.y = y;

		this.max = showmax ? max : Math.min(max, s.length);
		this.max = Math.min(max, s.length);
		nborder = new SMBorder(x, y, width, this.max * 12 + 2);
		buttons = new AutoButton[s.length];

		for (int i = 0; i < buttons.length; i++) {
			buttons[i] = new AutoButton(x + 1, y + 1 + 12 * i, s[i]);
		}
		refresh();
	}


	public void refresh() {
		if (cursor >= buttons.length) {
			cursor = 0;
		}
		if (cursor < 0) {
			cursor = buttons.length - 1;
		}

		while (cursor >= hcursor + max) {
			hcursor++;
		}
		while (cursor < hcursor) {
			hcursor--;
		}

		for (int i = hcursor; i < Math.min(hcursor + max, buttons.length); i++) {
			buttons[i].y = nborder.y + 1 + 12 * (i - hcursor);

			if (cursor == i) {
				buttons[i].setBordered(true);
			} else {
				buttons[i].setBordered(false);
			}
		}

		nborder.height = max * 12 + 2;
	}


	public static class SMBorder extends GmudWindow {

		public SMBorder(int x, int y, int width, int height) {
			super(x, y, width, height);
			bordered = true;
		}

		/* （非 Javadoc）
		 * @see lostland.gmud.exv2.ui.core.GmudWindow#draw()
		 */
		@Override
		public void draw() {
			drawBackground();
		}

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#onButtonDown(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	protected void onButtonDown(NewButton b) {
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#onButtonClick(lostland.gmud.exv2.ui.core.NewButton)
	 */
	@Override
	public void onButtonClick(NewButton b) {
		switch (b) {
			case NB_BACK:
				onCancel();
				break;
			case NB_ENTER:
				onClick(cursor);
				break;
			case NB_NULL:
				break;
			case NB_RIGHT:
			case NB_DOWN:
				cursor++;
				if (cursor >= buttons.length) {
					cursor = 0;
				}
				refresh();
				break;
			case NB_UP:
			case NB_LEFT:
				cursor--;
				if (cursor < 0) {
					cursor = buttons.length - 1;
				}
				refresh();
				break;
			default:
				break;

		}
	}


	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ButtonControlledScreen#show()
	 */
	@Override
	public void show() {
		drawbg();
		nborder.draw();
		for (int i = hcursor; i < Math.min(hcursor + max, buttons.length); i++) {
			buttons[i].draw();
		}
	}

}
